package com.wuchao.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import java.io.IOException;
import java.io.InputStream;


/**
 * @author 吴超66
 * @version 1.0
 * @date 2022/5/24 15:24
 */
public class Util {
    private  static SqlSessionFactory sf;//工厂对象
    //静态块
    static{
        SqlSessionFactoryBuilder sb=new SqlSessionFactoryBuilder();//工人
        //工人根据图纸，全局文件来建厂
        try {
            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
         sf=   sb.build(is);//工厂完毕
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession(){
        return  sf.openSession(true);//默认事务自动提交
    }
    public static void  closeSqlSession(SqlSession sqlSession){
        if(sqlSession!=null){
            sqlSession.close();
        }
    }
}
